package com.kmust.dream.wanderingdream.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmust.dream.wanderingdream.comm.dto.Result;
import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.comm.pojo.ReturnMessage;
import com.kmust.dream.wanderingdream.product.config.RedisConfig;
import com.kmust.dream.wanderingdream.product.entity.Product;
import com.kmust.dream.wanderingdream.product.utils.ReturnUtils;
import com.kmust.dream.wanderingdream.product.vo.ProductPageVO;
import com.kmust.dream.wanderingdream.product.service.ProductService;
import com.kmust.dream.wanderingdream.product.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {

    @Autowired(required = false)
    private ProductMapper productMapper;

    @Override
    public ResultBean getAllProducts() {
        List<Product> list = this.list();
        if (list == null || list.size() == 0)
            return Result.resultBean(ReturnMessage.GET_NULL.getCode(), ReturnMessage.GET_NULL.getMessage(), null);
        return Result.resultBean(ReturnMessage.GET_SUCCESS.getCode(), ReturnMessage.GET_SUCCESS.message, list);
    }

    @Override
    public ResultBean getProductsPages(Integer type, Integer current, Integer size) {
        if (size <= 0 || current < 0)
            return Result.resultBean(ReturnMessage.PAGE_FAIL.getCode(), ReturnMessage.PAGE_FAIL.getMessage(), "size或current非法");
        QueryWrapper<Product> condition = null;
        if (type != 0) {
            condition = new QueryWrapper<Product>()
                    .eq("product_category", type);
        }
        Page<Product> page = new Page<>(current, size);
        IPage<Product> productIPage = productMapper.selectPage(page, condition);
        ProductPageVO productPageVO = new ProductPageVO();
        productPageVO.setCurrent((int) productIPage.getCurrent()).setTotal(productIPage.getTotal()).setSize(productPageVO.getSize()).setProducts(productIPage.getRecords());
        return Result.resultBean(ReturnMessage.PAGE_SUCCESS.getCode(), ReturnMessage.PAGE_SUCCESS.getMessage(), productPageVO);
    }

    @Override
    @CachePut(value = RedisConfig.REDIS_KEY_DATABASE, key = "'product'+#product.productId")
    public ResultBean addProduct(Product product) {
        boolean save = this.save(product);
        if (save)
            return Result.resultBean(ReturnMessage.INSERT_SUCCESS.getCode(), ReturnMessage.INSERT_SUCCESS.message, true);
        return Result.resultBean(ReturnMessage.INSERT_FAIL.getCode(), ReturnMessage.INSERT_FAIL.message, false);
    }

    @Override
    @CacheEvict(value = RedisConfig.REDIS_KEY_DATABASE, key = "'product'+#product.productId")
    public ResultBean updateProduct(Product product) {
        boolean update = this.saveOrUpdate(product);
        if (update)
            return Result.resultBean(ReturnMessage.UPDATE_SUCCESS.getCode(), ReturnMessage.UPDATE_SUCCESS.getMessage(), true);
        return Result.resultBean(ReturnMessage.UPDATE_FAIL.getCode(), ReturnMessage.UPDATE_FAIL.getMessage(), false);
    }

    @Override
    @CacheEvict(value = RedisConfig.REDIS_KEY_DATABASE, key = "'product'+#productId")
    public ResultBean deleteProductById(Integer productId) {
        UpdateWrapper<Product> productUpdateWrapper = new UpdateWrapper<>();
        productUpdateWrapper.eq("product_id", productId).set("status", false);
        boolean update = this.update(productUpdateWrapper);
        if (update)
            return Result.resultBean(ReturnMessage.DELETE_SUCCESS.getCode(), ReturnMessage.DELETE_SUCCESS.getMessage(), true);
        return Result.resultBean(ReturnMessage.DELETE_FAIL.getCode(), ReturnMessage.DELETE_FAIL.getMessage(), false);
    }

    @Override
    @Cacheable(value = RedisConfig.REDIS_KEY_DATABASE, key = "'product'+#productId")
    public ResultBean getProductById(Integer productId) {
        System.out.println("进入数据库查询");
        return ReturnUtils.getOne(getById(productId));
    }

    @Override
    public ResultBean getAllProductByCategoryId(Integer categoryId) {
        QueryWrapper<Product> condition = new QueryWrapper<Product>()
                .eq("product_category", categoryId);
        return ReturnUtils.getList(list(condition));
    }

    @Override
    public ResultBean getMaxId() {
        List<Product> list = list();
        return ReturnUtils.getOne(list.get(list.size() - 1).getProductId());
    }
}




